/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        std::vector<int> res;
        if(!root)
            return res;

        std::queue<TreeNode*> cnt_layer;
        cnt_layer.push(root);

        while(!cnt_layer.empty())
        {
            
            int size = cnt_layer.size();
            for(int i = 0; i < size; i++)
            {
                TreeNode* cnt_root = cnt_layer.front();
                
                if(i == 0)
                    res.push_back(cnt_root->val);

                cnt_layer.pop();
                if(cnt_root->right)
                    cnt_layer.push(cnt_root->right);
                if(cnt_root->left)
                    cnt_layer.push(cnt_root->left);
                
            }
        }

        return res;
    }
};